<template>
	<view>
		<view class="list">
			<view class="list-call">
				<view class="" style="font-size: 22px;color:#5473e8;">注册服务</view>
				<input  class="u-input" type="text" v-model="myIp" maxlength="32" placeholder="例如:http://10.13.145.119:8092/wms/" value="admin"/>
			</view>
			<view class="list-call">
				<view class="" style="font-size: 22px;color:#5473e8;">特征码</view>
				<input :disabled="true" class="u-input" type="text" v-model="signature" maxlength="32" placeholder="" value="admin"/>
			</view>
			<view class="list-call">
				<view class="" style="font-size: 22px;color:#5473e8;">注册码</view>
				<input  class="u-input" type="text" v-model="registrationCode" maxlength="32" placeholder="请输入注册码" value="admin"/>
			</view>
			<view class="list-call">
				<view class="" style="font-size: 22px;color:#5473e8;">注册时间</view>
				<input  class="u-input" type="text" v-model="registrationInformation" maxlength="32" placeholder="比如:2022-01-01/2031-01-01" value="admin"/>
			</view>
			<view class="list-call">
				<view class="" style="font-size: 22px;color:#5473e8;">使用权限</view>
				<u-switch 
				active-color="green" inactive-color="red" 
				:disabled="disableTrue" v-model="isCanUse"></u-switch>
				<!-- <input  class="u-input" disabled="true" type="text" v-model="isCanUse" maxlength="32" placeholder="" value="admin"/> -->
			</view>
			
			<view class="button"   @click="registerBefore()"><text>注册和查询当前设备</text></view>
			<view class="button" :disabled="isSave"   @click="submit()"><text>保存</text></view>
			<view class="button"   @click="submit_30()"><text>试用30天</text></view>
			<view class="button"   @click="submit_30_base()"><text>注册后台并试用30天</text></view>
			<view class="button"   @click="clearStorage()"><text>清除缓存</text></view>
			
			<!-- <view class="button"   @click="submit()"><text>TEST</text></view>
			<view class="button"   @click="submitHttps()"><text>HTTP</text></view> -->
			<!-- <view class="button"   @click="setSessionStorage()"><text>setSessionStorage</text></view> -->
		
		
		</view>
	</view>
</template>

<script>

	export default{
		
		data(){
			return{
				// 是否能保存
				canSave:false,
				myIp:'http://zp450.gnway.cc/wms/',
				// myIp:'http://localhost:8092/wms/',
				disableTrue:true,
				isSave:true,
				baseIp:'',
				registerBeforeStatus:'',
				//特征码  注册成功后返回的特征码,有了特征码和时间区间,就可以存起来了
				signature:'862951031376081,862951031376081',
				registrationCode:'盐城_test',
				registrationInformation:'2022-01-01/2031-01-01',
				is_can_30_register:false,
				editForm: {
				        id: '',
				        remark: '',
				        // create_time: '',
						// 创建者
				        create_by: '',
						// 系统内置（Y是 N否）
				        config_type: 'N',
						 // 参数键值
				        config_value: '',
						 // 参数键名
				        config_key: '',
						// 参数名称
				        config_name: '',
						token:'adminTOKEN',
						equal_config_name:'',
				      },
					  isCanUse:false,
			}
		},
		onLoad() {
			const isCanUse =uni.getStorageSync('isCanUse');
			console.log("isCanUse:"+isCanUse)
			const USER_ID = uni.getStorageSync('USER_ID')
			
			this.editForm.create_by = USER_ID
			if(isCanUse==true){
				this.isCanUse = true
			}else{
				this.isCanUse = false
			}
			// 获取注册的时间
			const registered_time = uni.getStorageSync('registered_time')
			if(registered_time){
				this.registrationInformation= registered_time
			}
			
			
			uni.setStorageSync('connectionTest', false);
			
			this.getuuid()
			const baseService = uni.getStorageSync('baseService')
			// if(!baseService){
				// this.getId()
			// }
			this.submit_30()
			setTimeout(() => {
				uni.reLaunch({
					url: '/pages/sys/login/index'
				});
			}, 10);
		},
		methods:{
			registerBefore(row){
				
				const _this = this
				// if(this.isCanUse){
				// 	_this.$u.toast('该设备已经拥有使用权限,无需注册')
				// 	return;
				// }
				if(row!=30){
					console.log('长期注册进来的方法')
					this.refreshEdiftrom()
					this.editForm.config_value=this.registrationInformation
					// this.editForm.config_type='N'
					
				}
				// 注册码可以为注册申请的人,
				this.editForm.config_key=this.registrationCode
				// 特征码
				// this.editForm.config_name = this.signature
				this.editForm.equal_config_name = this.signature
				console.log(JSON.stringify(this.editForm))
				uni.request({
				    url: this.myIp+'VueSysConfig/getAllVueSysConfig', //仅为示例，并非真实接口地址。
				    // 设备号(特征码)去查询该机器
					data:{
						 // 参数键值
						// config_value: row!=30?this.registrationInformation:'2022-01-01/2031-01-01',
						 // 参数键名
						// config_key: this.registrationCode,
						// 设备号
						// config_name: '',
						equal_config_name:this.signature,
					},
				    header: {
						'token':'adminTOKEN'
				    },
				    success: (res) => {
				        console.log("res:"+JSON.stringify(res.data));
						let result = res.data
						if(result.success){
							let data = result.result.data
							console.log(data);
							if(data){
								let firstData = data[0]
								if(firstData){
									
									if(firstData.config_type==='Y'){
										_this.$u.toast('已审核,请保存,注册时间区间:'+
										firstData.config_value+',如果不认可此注册时间,请联系管理员进行修改')
										_this.isSave=false
										_this.canSave = true
										// 直接保存注册时间 2022-01-01/2031-01-01 
										uni.setStorageSync('registered_time',firstData.config_value)
										_this.registrationInformation = firstData.config_value
										
									}else{
										_this.$u.toast('已注册,请等待管理员审核')
										console.log('已注册,请等待管理员审核')
										if(row=='30'){
											_this.is_can_30_register = true
										}
										
									}
									_this.registerBeforeStatus = true
									return
								}
								
							}
							_this.register()
							_this.registerBeforeStatus = false
						}
				        
				    }
				});
			},
			register(){
				
				let _this= this
				// 注册码可以为注册申请的人,
				this.editForm.config_key=this.registrationCode
				this.editForm.config_name = this.signature
				if(this.is_can_30_register==true){
					// 注册时间
					this.editForm.config_value = this.registrationInformation
					
					this.editForm.config_type = 'N'
				}
				
				uni.request({
				    url: this.myIp+'VueSysConfig/setVueSysConfig', //仅为示例，并非真实接口地址。
				    data: {
				        json: JSON.stringify(this.editForm)
				    },
				    header: {
				        'custom-header': 'hello', //自定义请求头信息
						'token':'adminTOKEN'
				    },
				    success: (res) => {
				        console.log(res.data);
						let data = res.data
						_this.$u.toast(data.msg)
						if(data.success){
							_this.getId()
						}
				        
				    }
				});

			},
			getuuid() {
				let _this= this
				
				console.log(' ' );
				// #ifdef APP-PLUS
				plus.device.getInfo({
					success: (e) => {
						console.log('getDeviceInfo success: ' + JSON.stringify(e));
						_this.signature = e.uuid;
						_this.$u.toast('获取特征码成功')
						return;
					},
					fail: (e) => {
						console.log('getDeviceInfo failed: ' + JSON.stringify(e));
							
									}
								});
								
				// #endif
				_this.$u.toast('请尝试切换为移动设备')
			},
			
			getId(){
				const _this = this
				this.refreshEdiftrom()
				// 注册码可以为注册申请的人,
				this.editForm.config_key=this.registrationCode
				// 注册时间
				this.editForm.config_value = this.registrationInformation
				this.editForm.config_name = this.signature
				uni.request({
				    url: this.myIp + 'VueSysConfig/getAllVueSysConfig', //仅为示例，并非真实接口地址。
				    data: 
				        this.editForm
				    ,
				    header: {
				        'custom-header': 'hello', //自定义请求头信息
						'token':'adminTOKEN'
				    },
				    success: (res) => {
				        console.log("res:"+JSON.stringify(res.data));
						let result = res.data
						if(result.success){
							let data = result.result.data
							console.log(data);
							if(data){
								
								let firstData = data[0]
								if(firstData){
									_this.isCanUse=firstData.config_type == 'Y'
									const baseService = uni.getStorageSync('baseService')
									if(baseService === ''){
										_this.editForm.id=firstData.id
										if(firstData.config_type == 'Y' || firstData.config_type == 'y'){
											// this.$u.toast('可以保存')
											_this.submit()
										}else{
											_this.$u.toast('请联系管理员进行审核')
										}
									}
									
								}
								
							}
						}
				        
				    }
				});
			},
			refreshEdiftrom(){
				this.editForm= {
				       id: '',
				       remark: '',
				       // create_time: '',
				       // 创建者
				       create_by: '',
				       // 系统内置（Y是 N否）
				       config_type: 'N',
				        // 参数键值
				       config_value: '',
				        // 参数键名
				       config_key: '',
				       // 参数名称
				       config_name: '',
				       token:'adminTOKEN',
				       equal_config_name:'',
				      }
			},
			submit(){
				if(this.isCanUse){
					this.$u.toast('该设备已经拥有使用权限,无需保存')
					return;
				}
				if(this.canSave){
					uni.setStorageSync('baseService', true);
					// 直接保存注册时间 2022-01-01/2031-01-01
					uni.setStorageSync('registered_time',this.registrationInformation)
					uni.setStorageSync('isCanUse',true)
					this.$u.toast('已保存')
					// this.isSave=true
					this.logout()
					this.isCanUse= true
				}else{
					this.$u.toast('还没有进行审核,请联系管理员进行注册审核')
				}
				
				// try {
				// 	// https://aidex.vip/api
				// 	uni.setStorageSync('baseUrl', this.baseIp);
				// 	// uni.setStorageSync('baseUrl', 'http://localhost:8092/wms');
				// } catch (e) {
				// 	// error
				// }
				// this.$u.toast('ip已经设置为:'+uni.getStorageSync('baseUrl'));
				// //刷新生效
				// this.$router.go(0)
			},
			submit_30(){
				var date1 = new Date();
				var date2 = new Date(date1);
				date2.setDate(date1.getDate() + 30);
				var dateBegin = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
				var dateEnd = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate()
				console.log(dateBegin );
				console.log(dateEnd);
				
				if(this.isCanUse){
					this.$u.toast('该设备已经拥有使用权限,无需试用')
					return;
				}
				uni.setStorageSync('baseService', true);
				// 直接保存注册时间 2022-01-01/2031-01-01
				uni.setStorageSync('registered_time',dateBegin+'/'+dateEnd)
				uni.setStorageSync('isCanUse',true)
				this.$u.toast('已保存,'+dateEnd+'为到期日期')
				this.isCanUse= true
				this.logout()
			},
			clearStorage(){
				uni.clearStorageSync();
				this.$u.toast('已清除')
				setTimeout(() => {
					
					this.$router.go(0)
				}, 100);
				
			},
			submit_30_base(){
				const _this=this
				var date1 = new Date();
				var date2 = new Date(date1);
				date2.setDate(date1.getDate() + 30);
				var dateBegin = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
				var dateEnd = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate()
				// console.log(dateBegin );
				// console.log(dateEnd);
				
				if(this.isCanUse){
					this.$u.toast('该设备已经拥有使用权限,无需试用')
					return;
				}
				// 修改一下时间,然后before检测一下
				this.editForm.config_value=dateBegin+'/'+dateEnd;
				console.log(this.editForm.config_value);
				this.editForm.remark = '试用30天'
				this.editForm.config_type = 'Y'
				
						_this.registerBefore(30)
				
				
				// setTimeout(() => {
				// 	if(_this.is_can_30_register==true){
				// 		_this.register()
				// 	}
				// }, 1000);
				// uni.setStorageSync('baseService', true);
				// // 直接保存注册时间 2022-01-01/2031-01-01
				// uni.setStorageSync('registered_time',dateBegin+'/'+dateEnd)
				// uni.setStorageSync('isCanUse',true)
				// this.$u.toast('已保存,'+dateEnd+'为到期日期')
				// this.isCanUse= true
				// this.logout()
			},
			submitHttps(){
				const _this = this
				uni.request({
				    url:  this.myIp+'user/getAllUserName', //仅为示例，并非真实接口地址。
				    method: 'GET',
					success: (res) => {
				        console.log("res:"+JSON.stringify(res.data));
						// let result = res.data
						// if(result.success){
							_this.$u.toast(JSON.stringify(res.data.msg))
						// }
				        
				    }
				});
			},
			logout() {
				// uni.setStorageSync('logout', true);
				// 多少带点停顿感
				setTimeout(() => {
					
					uni.reLaunch({
						url: '/pages/sys/login/index'
					});
				}, 1700);
			},
			setSessionStorage(){
				sessionStorage.setItem('hello','test');
			}
		}
	}
</script>
<style lang="scss">
@import 'index.scss';

	.logo {
		width: 80%;
		font-size:64rpx;
		color: #5473e8;
		margin: 80rpx auto 80rpx auto;
	}
	.list-call-icon{
		color: #ff0000;
	}
	.currentPhone-box{
		text-align: center;
		padding: 40rpx 80rpx;
		.number-text{
			color: #000000;
			font-size: 60rpx;
		}
		.other-text{
			color: #999999;
			font-size: 26rpx;
			padding: 20rpx 0;
		}
		.u-btn{
			margin: 30rpx auto;
		}
		.u-hairline-border{
		    border: 1px solid #fff;
		}
	}
	
	.register {
		display:inline-block;
		color: #5473e8;
		height: 40rpx;
		line-height: 40rpx;
		font-size: 28rpx;
		float:right;
		margin-top: 6rpx;
	}
	.register-link{
		float: right;
		padding: 0 16rpx;
	}
	.reg-link{
		display: inline-block;
		color: #5473e8;
	}
	.oauth2 {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		margin: 0rpx 100rpx 30rpx;
	
		image {
			height: 80rpx;
			width: 80rpx;
		}
	}
	.u-tabs{
		padding: 0 70rpx;
	}
</style>